<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>打印人类可读的变量信息</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.isset.html">« isset</a></li>
      <li style="float: right;"><a href="function.serialize.html">serialize »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.var.html">Variable handling 函数</a></li>
    <li>打印人类可读的变量信息</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.print-r" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">print_r</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">print_r</span> &mdash; <span class="dc-title">
   打印人类可读的变量信息
  </span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.print-r-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>print_r</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$return</code><span class="initializer"> = <strong><code>false</code></strong></span></span>): <span class="type"><span class="type">string</span>|<span class="type">bool</span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>print_r()</strong></span> 以人类可读的方式显示变量的信息。
  </p>
  <p class="para">
   <span class="function"><strong>print_r()</strong></span>、<span class="function"><a href="function.var-dump.html" class="function">var_dump()</a></span>、<span class="function"><a href="function.var-export.html" class="function">var_export()</a></span> 
   还将显示对象的 protected 和 private 属性。不会显示静态类成员。
  </p>
 </div>

   
 <div class="refsect1 parameters" id="refsect1-function.print-r-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">value</code></dt>

     <dd>

      <p class="para">
       要打印的表达式。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">return</code></dt>

     <dd>

      <p class="para">
       想要捕获 <span class="function"><strong>print_r()</strong></span> 的输出，请使用 <code class="parameter">return</code>
       参数。当此参数为 <strong><code>true</code></strong>，<span class="function"><strong>print_r()</strong></span> 将返回信息，而不是打印它。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.print-r-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   如果是指定 <span class="type">string</span>、<span class="type">int</span> 或 <span class="type">float</span>，会直接打印值本身。如果是指定
   <span class="type">array</span>，值将以键和元素的格式显示。<span class="type">object</span> 也一样。
  </p>
  <p class="para">
   当 <code class="parameter">return</code> 参数设置成 <strong><code>true</code></strong>，本函数会返回 <span class="type">string</span>。否则返回 <strong><code>true</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.print-r-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5298">
    <p><strong>示例 #1 <span class="function"><strong>print_r()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
&lt;pre&gt;<br /><span style="color: #0000BB">&lt;?php<br />$a&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #DD0000">'a'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'b'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'banana'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'c'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">print_r&nbsp;</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;<br /></span>&lt;/pre&gt;</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
&lt;pre&gt;
Array
(
    [a] =&gt; apple
    [b] =&gt; banana
    [c] =&gt; Array
        (
            [0] =&gt; x
            [1] =&gt; y
            [2] =&gt; z
        )
)
&lt;/pre&gt;
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-5299">
    <p><strong>示例 #2 <code class="parameter">return</code> 参数的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$b&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(</span><span style="color: #DD0000">'m'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'monkey'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'foo'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'bar'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'x'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array&nbsp;(</span><span style="color: #DD0000">'x'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'z'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$results&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;$results&nbsp;包含了&nbsp;print_r&nbsp;的输出<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.print-r-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <p class="para">When the <code class="parameter">return</code> parameter
is used, this function uses internal output buffering prior to PHP 7.1.0, so it cannot be used inside an
<span class="function"><a href="function.ob-start.html" class="function">ob_start()</a></span> callback function.</p></p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.print-r-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.ob-start.html" class="function" rel="rdfs-seeAlso">ob_start()</a> - 打开输出控制缓冲</span></li>
    <li class="member"><span class="function"><a href="function.var-dump.html" class="function" rel="rdfs-seeAlso">var_dump()</a> - 打印变量的相关信息</span></li>
    <li class="member"><span class="function"><a href="function.var-export.html" class="function" rel="rdfs-seeAlso">var_export()</a> - 输出或返回变量的可解析字符串表示</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>